package com.example.demo3.controller;


import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.Tool;
import com.example.demo3.entity.DictData;
import com.example.demo3.service.ToolService;
import com.example.demo3.service.DictTypeService;
import com.example.demo3.utils.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工具 前端控制器
 * </p>
 *
 * @author xsyuan
 * @since 2020-03-27
 */
@Controller
@RequestMapping("tool")
public class ToolController extends BaseController {

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private ToolService toolService;


    @RequestMapping("list")
    public void list(){}

    @RequestMapping("toolPageInfo")
    @ResponseBody
    public DataPageMessage toolPageInfo(int pageindex, String orderdir, int pagesize, String ordercolumn, int sEcho, String textVague, String codeVague, String nameVague){
        Map<Object,Object> map = new HashMap<>();
            map.put("pageindex",pageindex);
            map.put("pagesize",pagesize);
            map.put("orderdir",orderdir);
            map.put("ordercolumn",ordercolumn);
        if(StringUtils.isNotBlank(textVague)){
            map.put("textVague",textVague);
        }
        if(StringUtils.isNotBlank(codeVague)){
            map.put("codeVague",codeVague);
        }
        if(StringUtils.isNotBlank(nameVague)){
            map.put("nameVague",nameVague);
        }
        int countPage=toolService.countPage(map);
        List<Tool> resourceDeviceList=toolService.queryPage(map);
        return DataPageMessage.ok(resourceDeviceList,countPage,countPage,sEcho,"");
    }

    @RequestMapping("add")
    public String add (Model model){
        List<DictData> dictDataList =dictTypeService.getByType(Constants.DICT_TYPE_WORK_TOOL_TYPE);
        model.addAttribute("dictDataList",dictDataList);
        return "tool/add";
    }

    @RequestMapping("edit")
    public String edit (Model model, String id){
        if(StringUtils.isNotBlank(id)){
            Map<String,Object> map  = new HashMap<>();
                map.put("id",id);
                map.put("delFlag",0);
            List<Tool> toolList = toolService.getByMap(map);
            if(toolList.size() >0 && toolList != null && !toolList.isEmpty()){
                model.addAttribute("tool",toolList.get(0));
            }
        }
        return "tool/edit";
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseMessage del (Tool tool){
        tool.setDelFlag(1);
        toolService.update(tool);
        return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public ResponseMessage saveOrUpdate(Tool tool){
        tool.setCreateUser("xsyuan");
        tool.setCreateTime(new Date());
        String msg= "";
        if(StringUtils.isNotBlank(tool.getId())){
            tool.setUpdateUser("xsyuan");
            tool.setUpdateTime(new Date());
            toolService.update(tool);
            msg = "修改成功";
        }else{
            tool.setCreateUser("xsyuan");
            tool.setCreateTime(new Date());
            toolService.insert(tool);
            msg = "添加成功";
        }
        return ResponseMessage.ok(msg);
    }

}
